package com.runshu.newsmartark.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.runshu.newsmartark.R;
import com.runshu.newsmartark.base.BaseActivity;
import com.runshu.newsmartark.common.ConstantUrl;
import com.runshu.newsmartark.dbutil.DBUtils;
import com.runshu.newsmartark.dbutil.LogListEntity;
import com.runshu.newsmartark.dbutil.OperRecordfEntity;
import com.runshu.newsmartark.dbutil.RefreshUtils;
import com.runshu.newsmartark.entity.MqttBean;
import com.runshu.newsmartark.event.Event;
import com.runshu.newsmartark.event.EventBusUtils;
import com.runshu.newsmartark.event.EventCode;
import com.runshu.newsmartark.fragment.FaceManagerFragment;
import com.runshu.newsmartark.fragment.LogsManagerFragment;
import com.runshu.newsmartark.fragment.PhysicalManagerFragment;
import com.runshu.newsmartark.fragment.SeetingManagerFragment;
import com.runshu.newsmartark.fragment.WarnManagerFragment;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.network.netinterface.MyCallBack;
import com.runshu.newsmartark.smartbox.SmartBoxHelper;
import com.runshu.newsmartark.util.LodDialogClass;
import com.runshu.newsmartark.util.TextDrawableUtils;
import com.runshu.newsmartark.util.common.SharedPreferencesUtils;
import com.runshu.newsmartark.util.widget.TopTitleBar;
import com.runshu.newsmartark.util.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/8/26
 * @decs：
 */

public class ManagerActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    TopTitleBar mToolbar;
    @BindView(R.id.frag_social_rb_warm)
    RadioButton mFragSocialRbWarm;
    @BindView(R.id.frag_social_rb_face)
    RadioButton mFragSocialRbFace;
    @BindView(R.id.frag_social_rb_logs)
    RadioButton mFragSocialRbLogs;
    @BindView(R.id.frag_social_bottom_rg)
    RadioGroup mFragSocialBottomRg;
    @BindView(R.id.frag_social_container)
    ViewPager mFragSocialContainer;
    @BindView(R.id.tv_refresh_time)
    TextView mTvRefreshTime;
    @BindView(R.id.tv_pan_dian)
    TextView tvPanDian;
    private List<Fragment> mFragments;
    private OnRefreshActivityListener onRefreshActivityListener;

    @Override
    protected int getLayoutId() {
        return R.layout.act_manager;
    }

    @Override
    protected void initView() {
        mToolbar.setIsShowBac(true);
        mToolbar.setTitle("管理");
        mToolbar.setBacOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                EventBusUtils.sendEvent(new Event(EventCode.showExitDia));
            }
        });
    }

    @Override
    protected void initData() {
        mFragments = new ArrayList<>();
        LogsManagerFragment logsManagerFragment = new LogsManagerFragment();
        mFragments.add(logsManagerFragment);
        mFragments.add(new WarnManagerFragment());
//        mFragments.add(new PhysicalManagerFragment());
        mFragments.add(new FaceManagerFragment());
        mFragments.add(new SeetingManagerFragment());
        onRefreshActivityListener = logsManagerFragment;
        mFragSocialContainer.setOffscreenPageLimit(4);
        mFragSocialContainer.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
        mFragSocialBottomRg.check(R.id.frag_social_rb_logs);

        refreView();
    }

    private void refreView() {


        new Thread(new Runnable() {
            @Override
            public void run() {
                List<LogListEntity> logListInfo = DBUtils.getInstance().getProAllDao().getLogList();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvRefreshTime.setText("数据同步时间：" + (String) SharedPreferencesUtils.get(context, ConstantUrl.Share_RefreshTime, "暂无"));
                        if (logListInfo == null || logListInfo.size() == 0) {
                            mTvRefreshTime.setClickable(false);
                            TextDrawableUtils.setDrawableRight(context, mTvRefreshTime, R.mipmap.icon_not_refersh_data);
                        } else {
                            mTvRefreshTime.setClickable(true);
                            TextDrawableUtils.setDrawableRight(context, mTvRefreshTime, R.mipmap.icon_refresh_data);
                        }
                    }
                });

            }
        }).start();
    }

    @Override
    protected void bindListener() {
        mFragSocialBottomRg.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.frag_social_rb_warm:

                    mFragSocialContainer.setCurrentItem(1, false);
                    break;
                case R.id.frag_social_rb_face:

                    mFragSocialContainer.setCurrentItem(2, false);
                    break;
                case R.id.frag_social_rb_logs:

                    mFragSocialContainer.setCurrentItem(0, false);
                    break;

                case R.id.frag_social_rb_seeting:
                    mFragSocialContainer.setCurrentItem(3, false);

                    break;

            }
        });
    }

    @Override
    protected void afterRequestPermission(int requestCode, boolean isAllGranted) {

    }



    @OnClick(R.id.tv_pan_dian)
    //盘点模式
    public void onClick() {
        SmartBoxHelper.getInstance().openLightAndDoor(this, 0);
        Intent intent = new Intent(context, TakeStockDtaActivity.class);
        intent.putExtra("isPanMode", true);
        startActivity(intent);
        finish();
    }

    /**
     * 接口定义刷新同步数据
     **/
    public interface OnRefreshActivityListener {
        void onRefreshActivity();
    }


    //刷新数据库
    @OnClick(R.id.tv_refresh_time)
    public void onViewClicked() {
        LodDialogClass.showCustomCircleProgressDialog(context);
        LodDialogClass.setCustomCircleProgressDialogHintText("数据同步中...");
        RefreshUtils.getInstance().insertHardwareLog(new MyCallBack() {
            @Override
            public void onSuccess(ResponseBean data) {


                if (onRefreshActivityListener != null) {
                    onRefreshActivityListener.onRefreshActivity();
                }

                refreView();
                LodDialogClass.closeCustomCircleProgressDialog();
            }

            @Override
            public void onError(ResponseBean data) {
                LodDialogClass.closeCustomCircleProgressDialog();
                refreView();
            }
        });

    }



}